package com.cal.calonecal.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Date;

@ConfigurationProperties(prefix = "jwt")
@Component
@Data
public class TokenUtil {
    private static String SECRET = "pjj1is5the789best5465pjj";
    private static Long expire = 86400000l;

    public static String generateToken(String uid) {
        Date now = new Date();
        Date expiration = new Date(now.getTime() + expire);
        return Jwts.builder()
                .setSubject(String.valueOf(uid))
                .setIssuedAt(now)
                .signWith(SignatureAlgorithm.HS512, SECRET)
                .setExpiration(expiration)
                .compact();
    }

    public static Claims getClaimsFromToken(String token) {
        Claims claims = null;
        try {
            claims = Jwts.parser().setSigningKey(SECRET).parseClaimsJws(token).getBody();
        } catch (Exception e) {
            System.out.println("Error while parsing JWT claims: " + e.getMessage());
        }
        return claims;
    }
}